package com.ymatou.iapi.godmch.testcase;

import java.util.HashMap;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.adminBindAgentBean;
import com.ymatou.iapi.godmch.parameter.approveAgentBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.mchInfo;
import com.ymatou.iapi.godmch.parameter.mchandparentmchBean;
import com.ymatou.iapi.godmch.parameter.parentInfo;
import com.ymatou.iapi.godmch.parameter.setShareTypeBean;
import com.ymatou.iapi.godmch.service.CreateCodeCall;
import com.ymatou.iapi.godmch.service.adminBindAgentCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.mchandparentmchCall;
import com.ymatou.iapi.godmch.service.setShareTypeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.mch_info;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;
/**
*获取商户和上级商户身份
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_mchandparentmch{
	private static mchandparentmchBean bean;
	private static mchandparentmchCall call;
	private static mch_infoWapper mchinfoWapper=new mch_infoWapper();
	private static mch_relationWapper relationWapper=new mch_relationWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商户和上级商户身份");
	}

	@Before
	public void caseUp() {
		bean=new mchandparentmchBean();
		call=new mchandparentmchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchandparentmch_001() {
		Logger.start(true,"获取商户和上级商户身份,验证商户id与上级商户id匹配,-代理模式 默认 1自主选货-父级是渠道返回成功");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			
			bean.setMchId(userId_agent);
			bean.setParentMchId(userId_channl);
			call.setData(bean);
			call.callService();			
			//验证点
			checkVrify( userId_channl,userId_agent,true);
	        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchandparentmch_001_1() {
		Logger.start(true,"获取商户和上级商户身份,验证商户id与上级商户id匹配,-代理模式2.自动继承-父级是渠道返回成功");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			
			CreateCodeBean createCodeBean = GodmchManageCallService.InitcreateCodeBeanForAgent(userId_channl);
			createCodeBean.setAgentModel(2);//2.自动继承
			CreateCodeCall createCall = GodmchManageCallService.createCodeCall(createCodeBean);
			
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			
			bean.setMchId(userId_agent);
			bean.setParentMchId(userId_channl);
			call.setData(bean);
			call.callService();			
			//验证点
			checkVrify( userId_channl,userId_agent,true);
	        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchandparentmch_002() {
		Logger.start(true,"获取商户和上级商户身份,验证商户id与上级商户id不匹配,返回失败");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			
			bean.setMchId(userId_agent);
			bean.setParentMchId(userId_channl+99999);
			call.setData(bean);
			call.callService();			
			//验证点
			checkVrify( userId_channl,userId_agent,false);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchandparentmch_002_1() {
		Logger.start(true,"获取商户和上级商户身份,验证一个商户id同时有两个代理渠道,传入其中一个代理商户id,返回成功");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent2=DGUserCallService.UserCreateCall(usercreateBean1);
			int[] agentId={userId_agent,userId_agent2};
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, agentId);
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			
			bean.setMchId(userId_agent);
			bean.setParentMchId(userId_channl);
			call.setData(bean);
			call.callService();			
			//验证点
			checkVrify( userId_channl,userId_agent,true);		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchandparentmch_003() {
		Logger.start(true,"获取商户和上级商户身份,验证商户id为空,返回失败,提示错误");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			
			bean.setMchId(null);
			bean.setParentMchId(userId_channl);
			call.setData(bean);
			call.callService();			
			//验证点
	        Logger.verifyEquals("4001", call.getStatus1(), "验证返回成功status值");
	        Logger.verifyEquals(true, call.getMessage().contains("商户id不能为空"), "验证返回成功Message值");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchandparentmch_004() {
		Logger.start(true,"获取商户和上级商户身份,验证上级商户id为空,返回失败");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			
			bean.setMchId(userId_agent);
			bean.setParentMchId(null);
			call.setData(bean);
			call.callService();			
			//验证点
	        Logger.verifyEquals("4001", call.getStatus1(), "验证返回成功status值");
	        Logger.verifyEquals(true, call.getMessage().contains("上级商户id不能为空"), "验证返回成功Message值");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchandparentmch_005() {
		Logger.start(true,"获取商户和上级商户身份,验证商户id与上级商户id的分享类型mchShareType不一致");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			//修改渠道商分享类型
			setShareTypeBean sbean= new setShareTypeBean();
			setShareTypeCall scall=new setShareTypeCall();
			sbean.setMchId(userId_channl);
			sbean.setMchShareType(2);
			sbean.setMchStatus(0);
			sbean.setRemark("test");
			scall.setData(sbean);
			scall.callService();		
			
			bean.setMchId(userId_agent);
			bean.setParentMchId(userId_channl);
			call.setData(bean);
			call.callService();			
			//验证点
			checkVrify( userId_channl,userId_agent,true);
	        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mchandparentmch_006() {
		Logger.start(true,"获取商户和上级商户身份,验证商户id与上级商户id匹配,-父级是独立商户渠道-返回成功");
		try {
			//商户id
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			createIndepMchBean createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			//运营绑定的代理
			adminBindAgentBean adminBindAgentBean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			adminBindAgentCall adminBindAgentCall=GodmchManageCallService.AdminBindAgent(adminBindAgentBean);
	        //审核通过
			approveAgentBean approveAgentBean=GodmchManageCallService.approveAgentBeanV2(adminBindAgentBean.getMchId(),adminBindAgentBean.getAgentId());
			GodmchManageCallService.ApproveAgentCall(approveAgentBean);
			
			bean.setMchId(adminBindAgentBean.getAgentId());
			bean.setParentMchId(adminBindAgentBean.getMchId());
			call.setData(bean);
			call.callService();			
			//验证点
			checkVrify( bean.getParentMchId(),bean.getMchId(),true);
	        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	/***
	 * 断言
	 * @param userId_channl 渠道ID，也就是父级
	 * @param userId_agent 代理上ID，自己
	 * @param isCheckParentInfo 是否断言父级
	 * @throws InterruptedException
	 */
	private void checkVrify(int userId_channl,int userId_agent,boolean isCheckParentInfo) throws InterruptedException
	{
		 //渠道
		HashMap<String,Object> map=new HashMap();
		map.put("mchId", userId_agent);
		mch_info info_channl= mchinfoWapper.selectby(map).get(0);
		
        Logger.verifyEquals("200", call.getStatus1(), "验证返回成功status值");
        Logger.verifyEquals("请求成功", call.getMessage(), "验证返回成功Message值");
        mchInfo mInfo=call.getMchInfo();
        Logger.verifyEquals(mInfo.getAgent(), info_channl.getAgent()==1?true:false, "验证数据库mch_info中的agent与返回的是否一致");
        Logger.verifyEquals(mInfo.getChannel(), info_channl.getChannels()==1?true:false, "验证数据库mch_info中的Channel与返回的是否一致");
        Logger.verifyEquals(mInfo.getVip(), info_channl.getVip()==1?true:false, "验证数据库mch_info中的Vip与返回的是否一致");
        Logger.verifyEquals(mInfo.getSeqId(), info_channl.getSeqId(), "验证数据库mch_info中的SeqId与返回的是否一致");
        Logger.verifyEquals(mInfo.getMchId(), userId_agent, "验证返回的MchId是否一致");
        Logger.verifyEquals(mInfo.getMchTitle(), info_channl.getMchTitle().isEmpty()?info_channl.getNickName():info_channl.getMchTitle(), "验证数据库mch_info中的MchTitle与返回的是否一致");
        Logger.verifyEquals(mInfo.getAvatar(), info_channl.getAvatar(), "验证数据库mch_info中的Avatar与返回的是否一致");
        Logger.verifyEquals(mInfo.getCurrency(), info_channl.getCurrency(), "验证数据库mch_info中的Currency与返回的是否一致");
        Logger.verifyEquals(mInfo.getCountry(), info_channl.getCountry(), "验证数据库mch_info中的Country与返回的是否一致");
		//代理
        if(isCheckParentInfo)
        {
			HashMap<String,Object> map2=new HashMap();
			map2.put("mchId", userId_channl);
			mch_info info_agent= mchinfoWapper.selectby(map2).get(0);
	        parentInfo pInfo=call.getParentInfo();
	        Logger.verifyEquals(pInfo.getAgent(), info_agent.getAgent()==1?true:false, "验证数据库mch_info中的agent与返回的是否一致");
	        Logger.verifyEquals(pInfo.getChannel(), info_agent.getChannels()==1?true:false, "验证数据库mch_info中的Channel与返回的是否一致");
	        Logger.verifyEquals(pInfo.getVip(), info_agent.getVip()==1?true:false, "验证数据库mch_info中的Vip与返回的是否一致");
	        Logger.verifyEquals(pInfo.getParentMchId(), userId_channl, "验证返回的userId_channl是否一致");
	        Logger.verifyEquals(pInfo.getMchTitle(), info_agent.getMchTitle()==""?info_agent.getNickName():info_agent.getMchTitle(), "验证数据库mch_info中的MchTitle与返回的是否一致");
	        //验证代理类型
	        HashMap<String,Object> map3=new HashMap();
	        map3.put("mchId", userId_agent);
	        map3.put("parentMchId", userId_channl);
	        mch_relation relation=relationWapper.selectby(map3).get(0);
	        Logger.verifyEquals(pInfo.getAgentType(), relation.getAgentType(), "验证数据库mch_info中的AgentType与返回的是否一致");
	        Logger.verifyEquals(pInfo.getEnableSubAgent(), relation.getAddSubAgent()==1?true:false, "验证数据库mch_info中的EnableSubAgent与返回的是否一致");
	        
	        Logger.verifyEquals(pInfo.getAgentModel()==0?1:pInfo.getAgentModel(), relation.getAgentModel(), "验证数据库mch_info中的AgentModel与返回的是否一致");
	        //agentModelText
	    	if(pInfo.getAgentModel()==1)
	           Logger.verifyEquals("自主选货",pInfo.getAgentModelText(), "验证AgentModelText与返回的是否一致");
	    	else if(pInfo.getAgentModel()==2)
	    		Logger.verifyEquals("自动继承",pInfo.getAgentModelText(), "验证AgentModelText与返回的是否一致");
            //mchType 新增  20200415
	    	Logger.verifyEquals(pInfo.getMchType(), info_agent.getMchType(), "验证数据库mch_info中的MchType与返回的是否一致");
        }
	}
}